package com.xiecong.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

//@Component
public class DemoInterceptor implements HandlerInterceptor {

    /**
     * 【常用】
     * 进入controller前执行
     * @param request current HTTP request
     * @param response current HTTP response
     * @param handler MethodHandler controller中的方法
     * @return boolean 返回true则放行，false则不放行拦截，就到此为止 就不执行controller的方法了
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle之前");
        return true;
    }

    /**
     * controller方法执行后执行
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle执行");
    }

    /**
     * 最终执行
     * 执行controller方法后，不管是否报错都会执行
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion之后");
    }
}
